<?php
require_once GALAXYTOOL_ROOT."/secret/includes/db_handler.inc.php";
require_once GALAXYTOOL_ROOT."/secret/version.php";

/**
 * Installation class required during Galaxytool installation process.
 * @author eX0du5
 *
 */
class GalaxytoolInstaller extends GenericSuperclass {

	private $mysql_engine = "MyISAM";

	public function __construct($dbcon, $mysql_engine="MyISAM") {
		parent::__construct($dbcon);
		$this->mysql_engine = $mysql_engine;
	}

	public static function write_config_file($dbusername, $dbpassword, $dbname, $dbhost, $dbprefix,
		$dbtablename="", $systemtablename="", $utablename="", $playertable="", $allytable="", $noticetable="", $reporttable="",
	 	$iptablename="", $shouttable="", $allyhistory="", $playerhistory="", $fleetmovetable="",
	 	$player_activity="", $messagetable="", $combattable="", $combatparty="", $reportarchive="",
	 	$formtable="",$tablenames_include_suffix=false) {

		$dbusername = strip_tags($dbusername);
		$dbpassword = strip_tags($dbpassword);
		$dbname     = strip_tags($dbname);
		$dbhost     = strip_tags($dbhost);
		$dbprefix   = strip_tags($dbprefix);

		// remove DB prefix from beginning of every DB table
		if ($tablenames_include_suffix === true) {
			$dbtablename = str_replace($dbprefix, "", $dbtablename);
			$systemtablename = str_replace($dbprefix, "", $systemtablename);
			$utablename = str_replace($dbprefix, "", $utablename);
			$playertable = str_replace($dbprefix, "", $playertable);
			$allytable = str_replace($dbprefix, "", $allytable);
			$noticetable = str_replace($dbprefix, "", $noticetable);
			$reporttable = str_replace($dbprefix, "", $reporttable);
			$iptablename = str_replace($dbprefix, "", $iptablename);
			$shouttable = str_replace($dbprefix, "", $shouttable);
			$allyhistory = str_replace($dbprefix, "", $allyhistory);
			$playerhistory = str_replace($dbprefix, "", $playerhistory);
			$fleetmovetable = str_replace($dbprefix, "", $fleetmovetable);
			$player_activity = str_replace($dbprefix, "", $player_activity);
			$messagetable = str_replace($dbprefix, "", $messagetable);
			$combattable = str_replace($dbprefix, "", $combattable);
			$combatparty = str_replace($dbprefix, "", $combatparty);
			$reportarchive = str_replace($dbprefix, "", $reportarchive);
			$formtable = str_replace($dbprefix, "", $formtable);
		}

		if (empty($dbtablename))     $dbtablename     = "galaxy";
		if (empty($systemtablename)) $systemtablename = "system";
		if (empty($utablename))      $utablename      = "usertable";
		if (empty($playertable))     $playertable     = "players";
		if (empty($allytable))       $allytable       = "alliances";
		if (empty($noticetable))     $noticetable     = "notices";
		if (empty($reporttable))     $reporttable     = "reports";
		if (empty($iptablename))     $iptablename     = "iptable";
		if (empty($shouttable))      $shouttable      = "shoutbox";
		if (empty($allyhistory))     $allyhistory     = "alliances_history";
		if (empty($playerhistory))   $playerhistory   = "players_history";
		if (empty($fleetmovetable))  $fleetmovetable  = "fleet_movements";
		if (empty($player_activity)) $player_activity = "player_activity";
		if (empty($messagetable))    $messagetable    = "messages";
		if (empty($combattable))     $combattable     = "combat_reports";
		if (empty($combatparty))     $combatparty     = "combat_party";
		if (empty($reportarchive))   $reportarchive   = "report_archive";
		if (empty($formtable))       $formtable       = "form_saves";


		$data = '<?php
/**
 * DO NOT EDIT THIS FILE WITH NOTEPAD - USE WORDPAD INSTEAD!
 */

define(\'GALAXYTOOL_ROOT\',\''.GALAXYTOOL_ROOT.'\');
define(\'GALAXYTOOL_INSTALLED_VERSION\',\''.VERSION.'\');

// Database connection
$dbusername    = \''.$dbusername.'\'; // database username
$dbpassword    = \''.$dbpassword.'\'; // database password
$dbname        = \''.$dbname.'\';     // database name
$dbhost        = \''.$dbhost.'\';     // host address of your mysql server
$dbprefix      = \''.$dbprefix.'\';   // prefix for all database tablenames if you need one

// Database tables
$dbtablename     = $dbprefix . "'.$dbtablename.'";
$systemtablename = $dbprefix . "'.$systemtablename.'";
$utablename      = $dbprefix . "'.$utablename.'";
$playertable     = $dbprefix . "'.$playertable.'";
$allytable       = $dbprefix . "'.$allytable.'";
$noticetable     = $dbprefix . "'.$noticetable.'";
$reporttable     = $dbprefix . "'.$reporttable.'";
$iptablename     = $dbprefix . "'.$iptablename.'";
$shouttable      = $dbprefix . "'.$shouttable.'";
$allyhistory     = $dbprefix . "'.$allyhistory.'";
$playerhistory   = $dbprefix . "'.$playerhistory.'";
$fleetmovetable  = $dbprefix . "'.$fleetmovetable.'";
$player_activity = $dbprefix . "'.$player_activity.'";
$messagetable    = $dbprefix . "'.$messagetable.'";
$combattable     = $dbprefix . "'.$combattable.'";
$combatparty     = $dbprefix . "'.$combatparty.'";
$reportarchive   = $dbprefix . "'.$reportarchive.'";
$formtable       = $dbprefix . "'.$formtable.'";

/*
 * Do not change anything below this line
 */

// php5.3 requires time zone to be defined to avoid warnings - we explicitly want to use the server time zone here to be in sync with mysql
@date_default_timezone_set(date_default_timezone_get());

$global_mysql_con = mysql_connect($dbhost,$dbusername,$dbpassword);
mysql_select_db($dbname,$global_mysql_con);
mysql_query("SET NAMES \'utf8\';");
mysql_query("SET CHARACTER SET \'utf8\';");
mysql_query("SET SESSION collation_connection = \'utf8_general_ci\';");

?>';
		return file_put_contents(GALAXYTOOL_ROOT."/config/config.php", $data);
	}

	public static function write_attribute_file($owner, $maxshouts,$email_to,$email_from,$usePHPMailer,$default_language,$global_universe,$def_to_debris,$debris_rate,$speed_rate,$ogame_url,$SMPT_host,$SMPT_user,$SMPT_pass,$SMPT_secure,$SMPT_port) {
		$usePHPMailer  = ($usePHPMailer) ? "true" : "false";
		$def_to_debris = ($def_to_debris) ? "true" : "false";

		$owner            = strip_tags($owner);
		$maxshouts        = intval($maxshouts);
		$email_to         = strip_tags($email_to);
		$email_from       = strip_tags($email_from);
		$default_language = strip_tags($default_language);
		$global_universe  = strip_tags($global_universe);
		$debris_rate      = intval($debris_rate);
		$speed_rate       = intval($speed_rate);
		$ogame_url        = strip_tags($ogame_url);
		if ($usePHPMailer == "true") {
			$SMPT_host        = strip_tags($SMPT_host);
			$SMPT_user        = strip_tags($SMPT_user);
			$SMPT_pass        = strip_tags($SMPT_pass);
			$SMPT_secure      = strip_tags($SMPT_secure);
			$SMPT_port        = intval($SMPT_port);
		} else {
			$SMPT_host        = "smtp.prov.dom";
			$SMPT_user        = "user@prov.dom";
			$SMPT_pass        = "password";
			$SMPT_secure      = "";
			$SMPT_port        = "";
		}

		if ($ogame_url != "" && GalaxytoolInstaller::is_valid_url($ogame_url) === false) return false;

		$data = '<?php

define(\'OGAME_URL\',\''.$ogame_url.'\');

/**
 * Set your name here. It will be shown at the logon page.
 */
$owner       = \''.$owner.'\';

/**
 * Settings for shoutbox = maximum number of shout entries
 *
 */
$maxshouts   = '.$maxshouts.';

/**
 * The user at this address will be informed about new registrations
 */
$email_to   = \''.$email_to.'\';

/**
 * This address will be shown as sender of the email
 */
$email_from = \''.$email_from.'\';

/**
 * settings for phpmailer - only needed if you server cannot send emails
 */
$usePHPMailer        = '.$usePHPMailer.';	// use phpmailer ? (true | false)
$phpmailerHost       = "'.$SMPT_host.'";	// smtp hostname
$phpmailerUser       = "'.$SMPT_user.'";	// smtp username
$phpmailerPass       = "'.$SMPT_pass.'";	// smtp password
$phpmailerSMTPSecure = "'.$SMPT_secure.'";	// smtp authentication method (ssl)
$phpmailerPort       = "'.$SMPT_port.'";	// smtp port - enter number directly - without "" !

/**
 * choose your default language
* the following are available:
* balkan,brazilian,bulgarian,chinese,croatian,czech,danish,dutch,english,finnish,french,german,greek,
* hungarian,italian,japan,korean,latvian,lithuanian,norwegian,polish,portugues,romanian,russian,
* serbian,slovak,slovenian,swedish,spanish,taiwan,turkish
 */
$default_language = \''.$default_language.'\';


/**
 * Don\'t forget to enter this since only data from this universe can be uploaded
 */
$global_universe = \''.$global_universe.'\';

$def_to_debris = '.$def_to_debris.'; // set to true for ogame universe where attacked defense creates a debris field e.g. German U15
$debris_rate = '.$debris_rate.';      // 30% is default for most universes (only used to be sent to online sims like Speedsim and Dragosim)
$speed_rate  = '.$speed_rate.';       // increase this value if you are in a speed universe to get correct calculated VALUES for resources / 24 hours (default value is 1 )

?>';

		return file_put_contents(GALAXYTOOL_ROOT."/config/attributes.php", $data);
	}

	/**
	 * Create the admin account for the Galaxytool. Must be called after the database tables have been created.
	 * @param String $admin_password
	 * @param String $utablename
	 */
	public function create_admin_account($admin_password, $utablename) {
		$admin_password = trim(stripslashes($admin_password));

		$password_salt = substr(md5(time()),rand(0,26),10);
		$password_hash = md5( $password_salt . md5($admin_password) . md5($password_salt) );

		$query = "INSERT INTO $utablename
			  (id, username, userpass, password_salt, stylepath, is_admin, cansearch, caninsert, candelete, statusview, probeview, userman, allyhistory, diplomatic, fleetinfo, activities, maintenance, status, links) VALUES
			  (1, 'admin', '$password_hash', '$password_salt','styles.css', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'active', 'Galaxytool;http://www.galaxytool.eu')";
		$this->run_mysql_query($query);
	}

	/**
	 * Create all database tables required for the Galaxytool. It will also create the trigger statements.
	 * @param String $dbtablename
	 * @param String $systemtablename
	 * @param String $utablename
	 * @param String $playertable
	 * @param String $allytable
	 * @param String $noticetable
	 * @param String $reporttable
	 * @param String $iptablename
	 * @param String $shouttable
	 * @param String $allyhistory
	 * @param String $playerhistory
	 * @param String $fleetmovetable
	 * @param String $player_activity
	 * @param String $messagetable
	 * @param String $combattable
	 * @param String $combatparty
	 * @param String $reportarchive
	 * @param String $formtable
	 * @param String $dbprefix
	 */
	public function create_database_tables(
		$dbtablename, $systemtablename, $utablename, $playertable, $allytable, $noticetable, $reporttable,
	 	$iptablename, $shouttable, $allyhistory, $playerhistory, $fleetmovetable,
	 	$player_activity, $messagetable, $combattable, $combatparty, $reportarchive,
	 	$formtable, $dbprefix )
	 {

	 	$this->suppress_orignal_query_info(); // failed messages are good enough, no need to add orignal query to error object

	 	$this->create_galaxytable($dbtablename);
	 	$this->create_systemtable($systemtablename);
	 	$this->create_usertable($utablename);
	 	$this->create_player_table($playertable);
	 	$this->create_ally_table($allytable);
	 	$this->create_notice_table($noticetable);
	 	$this->create_report_table($reporttable);
	 	$this->create_ip_table($iptablename);
	 	$this->create_shout_table($shouttable);
	 	$this->create_allyhistory_table($allyhistory);
	 	$this->create_playerhistory_table($playerhistory);
	 	$this->create_fleetmove_table($fleetmovetable);
	 	$this->create_activity_table($player_activity);
	 	$this->create_message_table($messagetable);
	 	$this->create_combat_table($combattable);
	 	$this->create_combatparty_table($combatparty);
	 	$this->create_reportarchive_table($reportarchive);
	 	$this->create_form_table($formtable);

	 	// create trigger statements
	 	$this->create_playertable_trigger($playertable, $playerhistory, $dbprefix);
	 	$this->create_allytable_trigger($allytable, $allyhistory, $dbprefix);
	 	$this->create_reporttable_trigger($reporttable, $reportarchive, $playertable, $dbtablename, $dbprefix);

	 	// send feedback about installation (php and mysql version only)
	 	$this->send_feedback();

	}


	private function create_galaxytable($dbtablename) {
		$query = "CREATE TABLE $dbtablename (
				  galaxy int(2) NOT NULL default '0',
				  system int(3) NOT NULL default '0',
				  planet int(2) NOT NULL default '0',
				  ogame_playerid int(11) unsigned NOT NULL default '0',
				  moon enum('true','false') NOT NULL default 'false',
				  moonsize smallint(5) unsigned NOT NULL default '0',
				  metal int(10) unsigned NOT NULL default '0',
				  crystal int(10) unsigned NOT NULL default '0',
				  planetname varchar(40) NOT NULL default '',
				  PRIMARY KEY  (galaxy,system,planet),
				  KEY player_id (ogame_playerid)
	            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_systemtable($systemtablename) {
		$query = "CREATE TABLE $systemtablename (
				  galaxy int(2) unsigned NOT NULL,
				  system int(2) unsigned NOT NULL,
				  last_update datetime NOT NULL,
				  user_id smallint(5) unsigned NOT NULL default '0',
				  PRIMARY KEY (galaxy,system),
				  KEY (user_id)
	            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_usertable($utablename) {
		$query = "CREATE TABLE $utablename (
			  id smallint(10) unsigned NOT NULL auto_increment,
			  username varchar(40) NOT NULL default '',
			  userpass varchar(32) NOT NULL default '',
			  password_salt varchar(10) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
			  plugin_token varchar(32) DEFAULT NULL,
			  ogame_playerid int(11) unsigned NOT NULL default '0',
			  ogame_allyid int(11) unsigned NOT NULL default '0',
			  email varchar(50) NOT NULL default '',
			  language varchar(20) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
			  ingame varchar(40) NOT NULL default '',
			  allytag varchar(40) NOT NULL default '',
			  timezone_offset varchar(6) NOT NULL default '+00:00',
			  ogame_offset varchar(6) NOT NULL DEFAULT '+00:00',
			  getting_started enum('expand','collapse','hidden') NOT NULL DEFAULT 'expand',
  			  plugin_info enum('expand','collapse','hidden') NOT NULL DEFAULT 'expand',
			  stylepath varchar(255) NOT NULL default '',
			  is_admin enum('true','false') NOT NULL default 'false',
			  cansearch enum('true','false') NOT NULL default 'false',
			  caninsert enum('true','false') NOT NULL default 'false',
			  candelete enum('true','false') NOT NULL default 'false',
			  statusview enum('true','false') NOT NULL default 'false',
			  probeview enum('true','false') NOT NULL default 'false',
			  userman enum('true','false') NOT NULL default 'false',
			  allyhistory enum('true','false') NOT NULL default 'false',
			  diplomatic enum('true','false') NOT NULL default 'false',
			  fleetinfo enum('true','false') NOT NULL default 'false',
			  activities enum('true','false') NOT NULL default 'false',
			  maintenance enum('true','false') NOT NULL default 'false',
			  status enum('email','admin','active') NOT NULL default 'email',
			  logins smallint(5) unsigned NOT NULL default '0',
			  lastlogin datetime NOT NULL default '0000-00-00 00:00:00',
			  last_action datetime NOT NULL default '0000-00-00 00:00:00',
			  last_submit date NOT NULL default '0000-00-00',
			  search_results mediumint(8) unsigned NOT NULL default '25',
			  links text NOT NULL,
			  bb_coordinates varchar(25) NOT NULL default 'deeppink',
			  bb_moon varchar(25) NOT NULL default 'burlywood',
			  bb_debris varchar(25) NOT NULL default 'firebrick',
			  bb_ally varchar(25) NOT NULL default 'tomato',
			  bb_allydetails varchar(25) NOT NULL default 'coral',
			  bb_playername varchar(25) NOT NULL default 'teal',
			  bb_banned varchar(25) NOT NULL default 'red',
			  bb_vacation varchar(25) NOT NULL default 'skyblue',
			  bb_noob varchar(25) NOT NULL default 'limegreen',
			  bb_inactive varchar(25) NOT NULL default 'silver',
			  bb_longinactive varchar(25) NOT NULL default 'silver',
			  bb_outlaw varchar(25) NOT NULL default 'FF33FF',
			  PRIMARY KEY  (id),
			  UNIQUE KEY username (username),
			  KEY last_action (last_action),
			  KEY ingame (ingame),
			  KEY ogame_playerid (ogame_playerid),
			  KEY is_admin (is_admin)
            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_player_table($playertable) {
		$query = "CREATE TABLE $playertable (
			  id int(10) unsigned NOT NULL auto_increment,
			  playername varchar(40) NOT NULL default '',
			  ogame_playerid int(11) unsigned default NULL,
			  alliance_id int(10) unsigned NOT NULL default '0',
			  rank smallint(5) unsigned NOT NULL default '0',
			  points int(10) unsigned NOT NULL default '0',
			  frank smallint(5) unsigned NOT NULL default '0',
			  fpoints int(10) unsigned NOT NULL default '0',
			  rrank smallint(5) unsigned NOT NULL default '0',
			  rpoints int(10) unsigned NOT NULL default '0',
			  erank smallint(5) unsigned NOT NULL DEFAULT '0',
			  epoints int(10) unsigned NOT NULL DEFAULT '0',
			  hrank smallint(5) unsigned NOT NULL DEFAULT '0',
			  hpoints int(11) NOT NULL DEFAULT '0',
			  fbrank smallint(5) unsigned NOT NULL DEFAULT '0',
			  fbpoints int(10) unsigned NOT NULL DEFAULT '0',
			  fdrank smallint(5) unsigned NOT NULL DEFAULT '0',
			  fdpoints int(10) unsigned NOT NULL DEFAULT '0',
			  flrank smallint(5) unsigned NOT NULL DEFAULT '0',
			  flpoints int(10) unsigned NOT NULL DEFAULT '0',
			  ships int(11) unsigned NOT NULL DEFAULT '0',
			  last_stats_update datetime NOT NULL default '0000-00-00 00:00:00',
			  user_id smallint(5) unsigned NOT NULL default '0',
			  homegalaxy int(2) UNSIGNED NOT NULL default '0',
			  homesystem int(3) UNSIGNED NOT NULL default '0',
			  homeplanet int(2) UNSIGNED NOT NULL default '0',
			  noob enum('true','false') NOT NULL default 'false',
			  banned enum('true','false') NOT NULL default 'false',
			  vacation enum('true','false') NOT NULL default 'false',
			  inactive enum('true','false') NOT NULL default 'false',
			  long_inactive enum('true','false') NOT NULL default 'false',
			  outlaw enum('true','false') NOT NULL default 'false',
			  bandit enum('true','false') NOT NULL default 'false',
			  diplomatic_status enum('nap','ally','war','boycott','neutral','nothing','own','wing') NOT NULL default 'nothing',
			  spiolvl tinyint(3) unsigned NOT NULL default '0',
			  computech tinyint(3) unsigned NOT NULL default '0',
			  waffentech tinyint(3) unsigned NOT NULL default '0',
			  schildtech tinyint(3) unsigned NOT NULL default '0',
			  rpz tinyint(3) unsigned NOT NULL default '0',
			  energytech tinyint(3) unsigned NOT NULL default '0',
			  hypertech tinyint(3) unsigned NOT NULL default '0',
			  vbt tinyint(3) unsigned NOT NULL default '0',
			  impulse tinyint(3) unsigned NOT NULL default '0',
			  hra tinyint(3) unsigned NOT NULL default '0',
			  lasertech tinyint(3) unsigned NOT NULL default '0',
			  iontech tinyint(3) unsigned NOT NULL default '0',
			  plasmatech tinyint(3) unsigned NOT NULL default '0',
			  forschungsnetz tinyint(3) unsigned NOT NULL default '0',
			  expedition tinyint(3) unsigned NOT NULL default '0',
			  gravi tinyint(3) unsigned NOT NULL default '0',
			  last_tech_update datetime NOT NULL default '0000-00-00 00:00:00',
			  PRIMARY KEY  (id),
			  UNIQUE KEY ogame_playerid (ogame_playerid),
			  KEY alliance_id (alliance_id),
			  KEY noob (noob),
			  KEY banned (banned),
			  KEY vacation (vacation),
			  KEY inactive (inactive),
			  KEY playername (playername),
			  KEY rank (rank),
			  KEY frank (frank),
			  KEY last_stats_update (last_stats_update),
			  KEY diplomatic_status (diplomatic_status)
            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_ally_table($allytable) {
		$query = "CREATE TABLE $allytable (
			  id int(10) unsigned NOT NULL auto_increment,
			  ogame_allyid int(11) unsigned default NULL,
			  allyname varchar(40) default NULL,
			  rank smallint(5) unsigned NOT NULL default '0',
			  points int(10) unsigned NOT NULL default '0',
			  frank smallint(5) unsigned NOT NULL default '0',
			  fpoints int(10) unsigned NOT NULL default '0',
			  rrank smallint(5) unsigned NOT NULL default '0',
			  rpoints int(10) unsigned NOT NULL default '0',
			  erank smallint(5) unsigned NOT NULL DEFAULT '0',
			  epoints int(10) unsigned NOT NULL DEFAULT '0',
			  hrank smallint(5) unsigned NOT NULL DEFAULT '0',
			  hpoints int(11) NOT NULL DEFAULT '0',
			  fbrank smallint(5) unsigned NOT NULL DEFAULT '0',
			  fbpoints int(10) unsigned NOT NULL DEFAULT '0',
			  fdrank smallint(5) unsigned NOT NULL DEFAULT '0',
			  fdpoints int(10) unsigned NOT NULL DEFAULT '0',
			  flrank smallint(5) unsigned NOT NULL DEFAULT '0',
			  flpoints int(10) unsigned NOT NULL DEFAULT '0',
			  members smallint(5) unsigned NOT NULL default '0',
			  diplomatic_status enum('nap','ally','war','boycott','neutral','nothing','own','wing') default 'nothing',
			  last_update datetime default '0000-00-00 00:00:00',
			  user_id smallint(5) unsigned NOT NULL default '0',
			  PRIMARY KEY  (id),
			  UNIQUE KEY ogame_allyid (ogame_allyid),
			  KEY allyname (allyname),
			  KEY last_update (last_update),
			  KEY diplomatic_status (diplomatic_status),
			  KEY rank (rank)
            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_notice_table($noticetable) {
		$query = "CREATE TABLE $noticetable (
              id int(11) NOT NULL auto_increment,
              noticetext text NOT NULL,
              player_id int(10) unsigned NOT NULL default '0',
              last_update datetime NOT NULL default '0000-00-00 00:00:00',
              PRIMARY KEY  (id),
              UNIQUE KEY player_id (player_id)
            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_report_table($reporttable) {
		$query = "CREATE TABLE $reporttable (
			  id int(10) unsigned NOT NULL auto_increment,
			  galaxy int(2) unsigned NOT NULL default '0',
			  system int(3) unsigned NOT NULL default '0',
			  planet int(2) unsigned NOT NULL default '0',
			  planetname varchar(40) NOT NULL default '',
			  moon enum('true','false') NOT NULL default 'false',
			  msg_id int(10) unsigned default NULL,
			  metal int(10) unsigned NOT NULL default '0',
			  crystal int(10) unsigned NOT NULL default '0',
			  deuterium int(10) unsigned NOT NULL default '0',
			  energy int(10) unsigned NOT NULL default '0',
			  kt mediumint(8) unsigned NOT NULL default '0',
			  gt mediumint(8) unsigned NOT NULL default '0',
			  lj mediumint(8) unsigned NOT NULL default '0',
			  sj mediumint(8) unsigned NOT NULL default '0',
			  krz mediumint(8) unsigned NOT NULL default '0',
			  ss mediumint(8) unsigned NOT NULL default '0',
			  kolo mediumint(8) unsigned NOT NULL default '0',
			  rec mediumint(8) unsigned NOT NULL default '0',
			  spio mediumint(8) unsigned NOT NULL default '0',
			  bomb mediumint(8) unsigned NOT NULL default '0',
			  zerri mediumint(8) unsigned NOT NULL default '0',
			  ds mediumint(8) unsigned NOT NULL default '0',
			  skrz mediumint(8) unsigned NOT NULL default '0',
			  sat mediumint(8) unsigned NOT NULL default '0',
			  rak mediumint(8) unsigned NOT NULL default '0',
			  ll mediumint(8) unsigned NOT NULL default '0',
			  sl mediumint(8) unsigned NOT NULL default '0',
			  ion mediumint(8) unsigned NOT NULL default '0',
			  gauss mediumint(8) unsigned NOT NULL default '0',
			  plasma mediumint(8) unsigned NOT NULL default '0',
			  ksk mediumint(8) unsigned NOT NULL default '0',
			  gsk mediumint(8) unsigned NOT NULL default '0',
			  arak mediumint(8) unsigned NOT NULL default '0',
			  irak mediumint(8) unsigned NOT NULL default '0',
			  memi tinyint(3) unsigned NOT NULL default '0',
			  krimi tinyint(3) unsigned NOT NULL default '0',
			  deutsyn tinyint(3) unsigned NOT NULL default '0',
			  solar tinyint(3) unsigned NOT NULL default '0',
			  fusion tinyint(3) unsigned NOT NULL default '0',
			  robo tinyint(3) unsigned NOT NULL default '0',
			  nani tinyint(3) unsigned NOT NULL default '0',
			  rawe tinyint(3) unsigned NOT NULL default '0',
			  mesp tinyint(3) unsigned NOT NULL default '0',
			  krissp tinyint(3) unsigned NOT NULL default '0',
			  deutsp tinyint(3) unsigned NOT NULL default '0',
			  folab tinyint(3) unsigned NOT NULL default '0',
			  terra tinyint(3) unsigned NOT NULL default '0',
			  allydep tinyint(3) unsigned NOT NULL default '0',
			  raksilo tinyint(3) unsigned NOT NULL default '0',
			  mbase tinyint(3) unsigned NOT NULL default '0',
			  sensor tinyint(3) unsigned NOT NULL default '0',
			  sprungtor tinyint(3) unsigned NOT NULL default '0',
			  spiolvl tinyint(3) unsigned NOT NULL default '0',
			  computech tinyint(3) unsigned NOT NULL default '0',
			  waffentech tinyint(3) unsigned NOT NULL default '0',
			  schildtech tinyint(3) unsigned NOT NULL default '0',
			  rpz tinyint(3) unsigned NOT NULL default '0',
			  energytech tinyint(3) unsigned NOT NULL default '0',
			  hypertech tinyint(3) unsigned NOT NULL default '0',
			  vbt tinyint(3) unsigned NOT NULL default '0',
			  impulse tinyint(3) unsigned NOT NULL default '0',
			  hra tinyint(3) unsigned NOT NULL default '0',
			  lasertech tinyint(3) unsigned NOT NULL default '0',
			  iontech tinyint(3) unsigned NOT NULL default '0',
			  plasmatech tinyint(3) unsigned NOT NULL default '0',
			  forschungsnetz tinyint(3) unsigned NOT NULL default '0',
			  expedition tinyint(3) unsigned NOT NULL default '0' COMMENT 'Expedition technology',
			  gravi tinyint(3) unsigned NOT NULL default '0',
			  fleet_resis bigint(20) unsigned NOT NULL default '0',
			  defence_resis bigint(20) unsigned NOT NULL default '0',
			  scantime datetime NOT NULL default '0000-00-00 00:00:00',
			  user_id smallint(5) unsigned NOT NULL default '0',
			  scanned tinyint(3) unsigned NOT NULL DEFAULT '1',
			  min_phalanx int(3) unsigned default NULL,
			  max_phalanx int(3) unsigned default NULL,
			  min_rak int(3) unsigned default NULL,
			  max_rak int(3) unsigned default NULL,
			  PRIMARY KEY  (id),
			  UNIQUE KEY coordinates (galaxy,system,planet,moon),
			  KEY scantime (scantime),
			  KEY msg_id (msg_id),
			  KEY metal (metal),
			  KEY crystal (crystal),
			  KEY deuterium (deuterium),
			  KEY min_rak (galaxy,min_rak),
			  KEY min_phalanx (galaxy,min_phalanx),
			  KEY user_id (user_id)
           ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_ip_table($iptablename) {
		$query = "CREATE TABLE $iptablename (
              id int(10) unsigned NOT NULL auto_increment,
              userid smallint(5) unsigned NOT NULL default '0',
              ip varchar(15) NOT NULL default '',
              logintime datetime NOT NULL default '0000-00-00 00:00:00',
              PRIMARY KEY  (id),
              KEY userid (userid)
            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_shout_table($shouttable) {
		$query = "CREATE TABLE $shouttable (
              id mediumint(8) unsigned NOT NULL auto_increment,
              username varchar(40) NOT NULL default '',
              shouttext varchar(255) NOT NULL default '',
              shoutdate datetime NOT NULL default '0000-00-00 00:00:00',
              PRIMARY KEY  (id)
            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_allyhistory_table($allyhistory) {
		$query = "CREATE TABLE $allyhistory (
              alliance_id int(10) unsigned NOT NULL default '0',
              year smallint(5) unsigned NOT NULL default '0',
              month tinyint(3) unsigned NOT NULL default '0',
              day tinyint(3) unsigned NOT NULL default '0',
              rank smallint(5) unsigned NOT NULL default '0',
              points int(10) unsigned NOT NULL default '0',
              frank smallint(5) unsigned NOT NULL default '0',
              fpoints int(10) unsigned NOT NULL default '0',
              rrank smallint(5) unsigned NOT NULL default '0',
              rpoints int(10) unsigned NOT NULL default '0',
			  erank smallint(5) unsigned NOT NULL DEFAULT '0',
			  epoints int(10) unsigned NOT NULL DEFAULT '0',
              members smallint(5) unsigned NOT NULL default '0',
              PRIMARY KEY  (alliance_id,year,month,day)
            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_playerhistory_table($playerhistory) {
		$query = "CREATE TABLE $playerhistory (
              player_id int(10) unsigned NOT NULL default '0',
              alliance_id int(10) unsigned NOT NULL default '0',
              year smallint(5) unsigned NOT NULL default '0',
              month tinyint(3) unsigned NOT NULL default '0',
              day tinyint(3) unsigned NOT NULL default '0',
              rank smallint(5) unsigned NOT NULL default '0',
              points int(10) unsigned NOT NULL default '0',
              frank smallint(5) unsigned NOT NULL default '0',
              fpoints int(10) unsigned NOT NULL default '0',
              rrank smallint(5) unsigned NOT NULL default '0',
              rpoints int(10) unsigned NOT NULL default '0',
			  erank smallint(5) unsigned NOT NULL DEFAULT '0',
			  epoints int(10) unsigned NOT NULL DEFAULT '0',
              PRIMARY KEY  (player_id,year,month,day)
            ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_fleetmove_table($fleetmovetable) {
			$query = "CREATE TABLE $fleetmovetable (
					fleet_id int(10) unsigned NOT NULL,
					sub_fleet_id int(10) unsigned default NULL,
  					mission enum('expedition','colonization','recycle','transport','deployment','espionage','acs_defend','attack','acs_attack','moon_destruction','missile_attack') NOT NULL,
					arrival_time datetime NOT NULL default '0000-00-00 00:00:00',
					returning enum('true','false'),
					origin_galaxy int(2) NOT NULL default '0',
					origin_system int(3) NOT NULL default '0',
					origin_planet int(2) NOT NULL default '0',
					origin_planetname varchar(40) NOT NULL default '',
					origin_moon enum('true','false','unknown'),
					destination_galaxy int(2) NOT NULL default '0',
					destination_system int(3) NOT NULL default '0',
					destination_planet int(2) NOT NULL default '0',
				    destination_planetname varchar(40) NOT NULL default '',
				    destination_moon enum('true','false','unknown'),
  					scantime datetime NOT NULL default '0000-00-00 00:00:00',
				  	metal int(10) unsigned NOT NULL default '0',
				  	crystal int(10) unsigned NOT NULL default '0',
				  	deuterium int(10) unsigned NOT NULL default '0',
  					user_id smallint(5) unsigned NOT NULL default '0',
					kt mediumint(8) unsigned NOT NULL default '0',
					gt mediumint(8) unsigned NOT NULL default '0',
					lj mediumint(8) unsigned NOT NULL default '0',
					sj mediumint(8) unsigned NOT NULL default '0',
					krz mediumint(8) unsigned NOT NULL default '0',
					ss mediumint(8) unsigned NOT NULL default '0',
					kolo mediumint(8) unsigned NOT NULL default '0',
					rec mediumint(8) unsigned NOT NULL default '0',
					spio mediumint(8) unsigned NOT NULL default '0',
					bomb mediumint(8) unsigned NOT NULL default '0',
					zerri mediumint(8) unsigned NOT NULL default '0',
					ds mediumint(8) unsigned NOT NULL default '0',
					skrz mediumint(8) unsigned NOT NULL default '0',
					irak mediumint(8) unsigned NOT NULL default '0',
					PRIMARY KEY (fleet_id, sub_fleet_id),
					KEY origin (origin_galaxy,origin_system,origin_planet),
					KEY (arrival_time),
					KEY (user_id)
			      ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
			$this->run_mysql_query($query);
	}

	private function create_activity_table($player_activity) {
		$query = "CREATE TABLE $player_activity (
				id int(10) unsigned NOT NULL auto_increment,
	            player_id int(10) unsigned NOT NULL default '0',
				activity_date date NOT NULL default '0000-00-00',
	            hour tinyint(3) unsigned NOT NULL default '0',
	            minute tinyint(3) unsigned NOT NULL default '0',
	            weekday tinyint(3) unsigned NOT NULL default '0',
	            activity_type enum('galaxyview', 'scan', 'message', 'manual', 'combat_report', 'alliance_page') NOT NULL,
				PRIMARY KEY (id),
				UNIQUE KEY consistency (player_id, activity_date, hour, minute, activity_type)
		      ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_message_table($messagetable) {
		$query = "CREATE TABLE $messagetable (
				id int(10) unsigned NOT NULL auto_increment,
	            msg_id int(10) unsigned NOT NULL,
	            messagetime datetime NOT NULL default '0000-00-00 00:00:00',
	            subject varchar(255) NOT NULL default '',
	            player_id_from int(10) unsigned NOT NULL default '0',
	            player_id_to int(10) unsigned NOT NULL default '0',
				message_content TEXT NOT NULL,
	            userid smallint(5) unsigned NOT NULL default '0',
				PRIMARY KEY (id),
				KEY msg_id (msg_id),
				KEY userid (userid)
		      ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_combat_table($combattable) {
		$query = "CREATE TABLE $combattable (
				id int(10) unsigned NOT NULL auto_increment,
	            msg_id int(10) unsigned NOT NULL,
	            combattime datetime NOT NULL default '0000-00-00 00:00:00',
	            winner enum('attacker','defender','nobody') NOT NULL,
	            loot_metal int(10) unsigned,
		  		loot_crystal int(10) unsigned,
		  		loot_deuterium int(10) unsigned,
	            debris_metal int(10) unsigned,
		  		debris_crystal int(10) unsigned,
		  		public enum('true','false') default 'false',
		  		userid smallint(5) unsigned NOT NULL default '0',
				PRIMARY KEY (id),
				KEY msg_id (msg_id),
				KEY userid (userid)
		      ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_combatparty_table($combatparty) {
		$query = "CREATE TABLE $combatparty (
				id int(10) unsigned NOT NULL auto_increment,
				combat_id int(10) unsigned NOT NULL,
				combat_type enum('attacker','defender') NOT NULL,
				player_id int(10) unsigned,
		  		galaxy int(2) unsigned NOT NULL,
		  		system int(3) unsigned NOT NULL,
		  		planet int(2) unsigned NOT NULL,
		  		waffentech tinyint(3) unsigned,
		  		schildtech tinyint(3) unsigned,
		  		rpz tinyint(3) unsigned,
		  		lost_units bigint(20) unsigned DEFAULT NULL,
		  		attack_location enum('true','false') NOT NULL DEFAULT 'false',
				PRIMARY KEY (id),
				KEY combat_id (combat_id),
				KEY player_id (player_id)
		      ) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_reportarchive_table($reportarchive) {
		$query = "CREATE TABLE $reportarchive (
			  id int(10) unsigned NOT NULL AUTO_INCREMENT,
			  galaxy int(2) unsigned NOT NULL,
			  system int(3) unsigned NOT NULL,
			  planet int(2) unsigned NOT NULL,
			  moon enum('true','false') DEFAULT 'false',
			  msg_id int(10) unsigned DEFAULT NULL,
			  metal int(10) unsigned NOT NULL DEFAULT '0',
			  crystal int(10) unsigned NOT NULL DEFAULT '0',
			  deuterium int(10) unsigned NOT NULL DEFAULT '0',
			  energy int(10) unsigned NOT NULL DEFAULT '0',
			  kt mediumint(8) unsigned NOT NULL DEFAULT '0',
			  gt mediumint(8) unsigned NOT NULL DEFAULT '0',
			  lj mediumint(8) unsigned NOT NULL DEFAULT '0',
			  sj mediumint(8) unsigned NOT NULL DEFAULT '0',
			  krz mediumint(8) unsigned NOT NULL DEFAULT '0',
			  ss mediumint(8) unsigned NOT NULL DEFAULT '0',
			  kolo mediumint(8) unsigned NOT NULL DEFAULT '0',
			  rec mediumint(8) unsigned NOT NULL DEFAULT '0',
			  spio mediumint(8) unsigned NOT NULL DEFAULT '0',
			  bomb mediumint(8) unsigned NOT NULL DEFAULT '0',
			  zerri mediumint(8) unsigned NOT NULL DEFAULT '0',
			  ds mediumint(8) unsigned NOT NULL DEFAULT '0',
			  skrz mediumint(8) unsigned NOT NULL DEFAULT '0',
			  sat mediumint(8) unsigned NOT NULL DEFAULT '0',
			  rak mediumint(8) unsigned NOT NULL DEFAULT '0',
			  ll mediumint(8) unsigned NOT NULL DEFAULT '0',
			  sl mediumint(8) unsigned NOT NULL DEFAULT '0',
			  ion mediumint(8) unsigned NOT NULL DEFAULT '0',
			  gauss mediumint(8) unsigned NOT NULL DEFAULT '0',
			  plasma mediumint(8) unsigned NOT NULL DEFAULT '0',
			  ksk mediumint(8) unsigned NOT NULL DEFAULT '0',
			  gsk mediumint(8) unsigned NOT NULL DEFAULT '0',
			  arak mediumint(8) unsigned NOT NULL DEFAULT '0',
			  irak mediumint(8) unsigned NOT NULL DEFAULT '0',
			  memi tinyint(3) unsigned NOT NULL DEFAULT '0',
			  krimi tinyint(3) unsigned NOT NULL DEFAULT '0',
			  deutsyn tinyint(3) unsigned NOT NULL DEFAULT '0',
			  solar tinyint(3) unsigned NOT NULL DEFAULT '0',
			  fusion tinyint(3) unsigned NOT NULL DEFAULT '0',
			  robo tinyint(3) unsigned NOT NULL DEFAULT '0',
			  nani tinyint(3) unsigned NOT NULL DEFAULT '0',
			  rawe tinyint(3) unsigned NOT NULL DEFAULT '0',
			  mesp tinyint(3) unsigned NOT NULL DEFAULT '0',
			  krissp tinyint(3) unsigned NOT NULL DEFAULT '0',
			  deutsp tinyint(3) unsigned NOT NULL DEFAULT '0',
			  folab tinyint(3) unsigned NOT NULL DEFAULT '0',
			  terra tinyint(3) unsigned NOT NULL DEFAULT '0',
			  allydep tinyint(3) unsigned NOT NULL DEFAULT '0',
			  raksilo tinyint(3) unsigned NOT NULL DEFAULT '0',
			  mbase tinyint(3) unsigned NOT NULL DEFAULT '0',
			  sensor tinyint(3) unsigned NOT NULL DEFAULT '0',
			  sprungtor tinyint(3) unsigned NOT NULL DEFAULT '0',
			  spiolvl tinyint(3) unsigned NOT NULL DEFAULT '0',
			  computech tinyint(3) unsigned NOT NULL DEFAULT '0',
			  waffentech tinyint(3) unsigned NOT NULL DEFAULT '0',
			  schildtech tinyint(3) unsigned NOT NULL DEFAULT '0',
			  rpz tinyint(3) unsigned NOT NULL DEFAULT '0',
			  energytech tinyint(3) unsigned NOT NULL DEFAULT '0',
			  hypertech tinyint(3) unsigned NOT NULL DEFAULT '0',
			  vbt tinyint(3) unsigned NOT NULL DEFAULT '0',
			  impulse tinyint(3) unsigned NOT NULL DEFAULT '0',
			  hra tinyint(3) unsigned NOT NULL DEFAULT '0',
			  lasertech tinyint(3) unsigned NOT NULL DEFAULT '0',
			  iontech tinyint(3) unsigned NOT NULL DEFAULT '0',
			  plasmatech tinyint(3) unsigned NOT NULL DEFAULT '0',
			  forschungsnetz tinyint(3) unsigned NOT NULL DEFAULT '0',
			  expedition tinyint(3) unsigned NOT NULL DEFAULT '0',
			  gravi tinyint(3) unsigned NOT NULL DEFAULT '0',
			  scantime datetime DEFAULT NULL,
			  scanned tinyint(3) unsigned NOT NULL DEFAULT '1',
			  PRIMARY KEY (id),
			  UNIQUE KEY msg_id (msg_id),
			  KEY coordinates (galaxy,system,planet,moon)
			) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_form_table($formtable) {
		$query = "CREATE TABLE $formtable (
			id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			form_id VARCHAR( 30 ) NOT NULL ,
			save_id VARCHAR( 30 ) NOT NULL ,
			user_id SMALLINT( 10 ) UNSIGNED NOT NULL ,
			content TEXT NOT NULL,
			 UNIQUE KEY user_id (user_id,form_id,save_id)
			) ENGINE=".$this->mysql_engine." DEFAULT CHARSET=utf8";
		$this->run_mysql_query($query);
	}

	private function create_playertable_trigger($playertable, $playerhistory, $dbprefix) {
		// Player history - insert
		$query ="CREATE TRIGGER ".$dbprefix."players_ai AFTER INSERT ON $playertable
	  FOR EACH ROW BEGIN
	    INSERT INTO $playerhistory SET
	    	year = DATE_FORMAT(NOW(),'%Y'),
	    	month = DATE_FORMAT(NOW(),'%m'),
	    	day = DATE_FORMAT(NOW(),'%e'),
	    	player_id = NEW.id,
	    	alliance_id = NEW.alliance_id,
	    	rank = NEW.rank,
	    	points = NEW.points,
	    	frank = NEW.frank,
	    	fpoints = NEW.fpoints,
	    	rrank = NEW.rrank,
	    	rpoints = NEW.rpoints,
	    	erank = NEW.erank,
	    	epoints = NEW.epoints;
	    END";
		$this->run_mysql_query($query);

		$query ="CREATE TRIGGER ".$dbprefix."players_au AFTER UPDATE ON $playertable
	  FOR EACH ROW BEGIN
	    INSERT INTO $playerhistory SET
	    	year = DATE_FORMAT(NOW(),'%Y'),
	    	month = DATE_FORMAT(NOW(),'%m'),
	    	day = DATE_FORMAT(NOW(),'%e'),
	    	player_id = NEW.id,
	    	alliance_id = NEW.alliance_id,
	    	rank = NEW.rank,
	    	points = NEW.points,
	    	frank = NEW.frank,
	    	fpoints = NEW.fpoints,
	    	rrank = NEW.rrank,
	    	rpoints = NEW.rpoints,
	    	erank = NEW.erank,
	    	epoints = NEW.epoints
	    ON DUPLICATE KEY UPDATE
			rank=VALUES(rank), points=VALUES(points),
			frank=VALUES(frank), fpoints=VALUES(fpoints),
			rrank=VALUES(rrank), rpoints=VALUES(rpoints),
			erank=VALUES(erank), epoints=VALUES(epoints);
	  END";
		$this->run_mysql_query($query);
	}

	private function create_allytable_trigger($allytable, $allyhistory, $dbprefix) {
		// Ally history - insert
		$query ="CREATE TRIGGER ".$dbprefix."alliances_ai AFTER INSERT ON $allytable
	  FOR EACH ROW BEGIN
	    INSERT INTO $allyhistory SET
	    	year = DATE_FORMAT(NOW(),'%Y'),
	    	month = DATE_FORMAT(NOW(),'%m'),
	    	day = DATE_FORMAT(NOW(),'%e'),
	    	alliance_id = NEW.id,
	    	members = NEW.members,
	    	rank = NEW.rank,
	    	points = NEW.points,
	    	frank = NEW.frank,
	    	fpoints = NEW.fpoints,
	    	rrank = NEW.rrank,
	    	rpoints = NEW.rpoints,
	    	erank = NEW.erank,
	    	epoints = NEW.epoints;
	    END";
		$this->run_mysql_query($query);
		// Ally history - update
		$query ="CREATE TRIGGER ".$dbprefix."alliances_au AFTER UPDATE ON $allytable
	  FOR EACH ROW BEGIN
	    INSERT INTO $allyhistory SET
	    	year = DATE_FORMAT(NOW(),'%Y'),
	    	month = DATE_FORMAT(NOW(),'%m'),
	    	day = DATE_FORMAT(NOW(),'%e'),
	    	alliance_id = NEW.id,
	    	members = NEW.members,
	    	rank = NEW.rank,
	    	points = NEW.points,
	    	frank = NEW.frank,
	    	fpoints = NEW.fpoints,
	    	rrank = NEW.rrank,
	    	rpoints = NEW.rpoints,
	    	erank = NEW.erank,
	    	epoints = NEW.epoints
	    ON DUPLICATE KEY UPDATE
			rank=VALUES(rank), points=VALUES(points),
			frank=VALUES(frank), fpoints=VALUES(fpoints),
			rrank=VALUES(rrank), rpoints=VALUES(rpoints),
			erank=VALUES(erank), epoints=VALUES(epoints);
	  END";
		$this->run_mysql_query($query);
	}

	private function create_reporttable_trigger($reporttable, $reportarchive, $playertable, $dbtablename, $dbprefix) {
		$query ="CREATE TRIGGER ".$dbprefix."reports_ai AFTER INSERT ON $reporttable
		  FOR EACH ROW BEGIN
		   IF NEW.scanned = 1 OR NEW.computech > 0 OR NEW.energytech > 0 THEN
		    UPDATE $playertable SET
		     spiolvl = NEW.spiolvl,
		     computech = NEW.computech,
		     waffentech = NEW.waffentech,
		     schildtech = NEW.schildtech,
		     rpz = NEW.rpz,
		     energytech = NEW.energytech,
		     hypertech = NEW.hypertech,
		     vbt=NEW.vbt,
		     impulse = NEW.impulse,
		     hra=NEW.hra,
		     lasertech = NEW.lasertech,
		     iontech = NEW.iontech,
		     plasmatech = NEW.plasmatech,
		     forschungsnetz = NEW.forschungsnetz,
		     expedition = NEW.expedition,
		     gravi = NEW.gravi,
		     last_tech_update = NOW()
		     WHERE ogame_playerid = (SELECT g.ogame_playerid FROM $dbtablename g WHERE g.galaxy=NEW.galaxy AND g.system=NEW.system AND g.planet=NEW.planet);
		  END IF;

		  IF NEW.msg_id > 0 THEN
		    INSERT IGNORE INTO $reportarchive SET
		    galaxy = NEW.galaxy,
		    system = NEW.system,
		    planet = NEW.planet,
		    moon = NEW.moon,
		    msg_id = NEW.msg_id,
		    metal = NEW.metal,
		    crystal = NEW.crystal,
		    deuterium = NEW.deuterium,
		    energy = NEW.energy,
		    scantime = NEW.scantime,
		    scanned = NEW.scanned,

		    kt = NEW.kt,
		    gt = NEW.gt,
		    lj = NEW.lj,
		    sj = NEW.sj,
		    krz = NEW.krz,
		    ss = NEW.ss,
		    kolo = NEW.kolo,
		    rec = NEW.rec,
		    spio = NEW.spio,
		    bomb = NEW.bomb,
		    zerri = NEW.zerri,
		    ds = NEW.ds,
		    skrz = NEW.skrz,
		    sat = NEW.sat,

		    rak = NEW.rak,
		    ll = NEW.ll,
		    sl = NEW.sl,
		    ion = NEW.ion,
		    gauss = NEW.gauss,
		    plasma = NEW.plasma,
		    ksk = NEW.ksk,
		    gsk = NEW.gsk,
		    arak = NEW.arak,
		    irak = NEW.irak,

		    memi = NEW.memi,
		    krimi = NEW.krimi,
		    deutsyn = NEW.deutsyn,
		    solar = NEW.solar,
		    fusion = NEW.fusion,
		    robo = NEW.robo,
		    nani = NEW.nani,
		    rawe = NEW.rawe,
		    mesp = NEW.mesp,
		    krissp = NEW.krissp,
		    deutsp = NEW.deutsp,
		    folab = NEW.folab,
		    terra = NEW.terra,
		    allydep = NEW.allydep,
		    raksilo = NEW.raksilo,
		    mbase = NEW.mbase,
		    sensor = NEW.sensor,
		    sprungtor = NEW.sprungtor,

		    spiolvl = NEW.spiolvl,
		    computech = NEW.computech,
		    waffentech = NEW.waffentech,
		    schildtech = NEW.schildtech,
		    rpz = NEW.rpz,
		    energytech = NEW.energytech,
		    hypertech = NEW.hypertech,
		    vbt = NEW.vbt,
		    impulse = NEW.impulse,
		    hra = NEW.hra,
		    lasertech = NEW.lasertech,
		    iontech = NEW.iontech,
		    plasmatech = NEW.plasmatech,
		    forschungsnetz = NEW.forschungsnetz,
		    expedition = NEW.expedition,
		    gravi = NEW.gravi;

		  END IF;
		END";
		$this->run_mysql_query($query);

		$query ="CREATE TRIGGER ".$dbprefix."reports_au AFTER UPDATE ON $reporttable
	 FOR EACH ROW BEGIN
	 	IF NEW.scanned = 1 OR NEW.computech > 0 OR NEW.energytech > 0 THEN
		    UPDATE $playertable SET
		     spiolvl = NEW.spiolvl,
		     computech = NEW.computech,
		     waffentech = NEW.waffentech,
		     schildtech = NEW.schildtech,
		     rpz = NEW.rpz,
		     energytech = NEW.energytech,
		     hypertech = NEW.hypertech,
		     vbt=NEW.vbt,
		     impulse = NEW.impulse,
		     hra=NEW.hra,
		     lasertech = NEW.lasertech,
		     iontech = NEW.iontech,
		     plasmatech = NEW.plasmatech,
		     forschungsnetz = NEW.forschungsnetz,
		     expedition = NEW.expedition,
		     gravi = NEW.gravi,
		     last_tech_update = NOW()
		     WHERE ogame_playerid = (SELECT g.ogame_playerid FROM $dbtablename g WHERE g.galaxy=NEW.galaxy AND g.system=NEW.system AND g.planet=NEW.planet);
		END IF;

		IF NEW.msg_id > 0 THEN
		INSERT IGNORE INTO $reportarchive SET
		galaxy = NEW.galaxy,
		system = NEW.system,
		planet = NEW.planet,
		moon = NEW.moon,
		msg_id = NEW.msg_id,
		metal = NEW.metal,
		crystal = NEW.crystal,
		deuterium = NEW.deuterium,
		energy = NEW.energy,
		scantime = NEW.scantime,
		scanned = NEW.scanned,

		kt = NEW.kt,
		gt = NEW.gt,
		lj = NEW.lj,
		sj = NEW.sj,
		krz = NEW.krz,
		ss = NEW.ss,
		kolo = NEW.kolo,
		rec = NEW.rec,
		spio = NEW.spio,
		bomb = NEW.bomb,
		zerri = NEW.zerri,
		ds = NEW.ds,
		skrz = NEW.skrz,
		sat = NEW.sat,

		rak = NEW.rak,
		ll = NEW.ll,
		sl = NEW.sl,
		ion = NEW.ion,
		gauss = NEW.gauss,
		plasma = NEW.plasma,
		ksk = NEW.ksk,
		gsk = NEW.gsk,
		arak = NEW.arak,
		irak = NEW.irak,

		memi = NEW.memi,
		krimi = NEW.krimi,
		deutsyn = NEW.deutsyn,
		solar = NEW.solar,
		fusion = NEW.fusion,
		robo = NEW.robo,
		nani = NEW.nani,
		rawe = NEW.rawe,
		mesp = NEW.mesp,
		krissp = NEW.krissp,
		deutsp = NEW.deutsp,
		folab = NEW.folab,
		terra = NEW.terra,
		allydep = NEW.allydep,
		raksilo = NEW.raksilo,
		mbase = NEW.mbase,
		sensor = NEW.sensor,
		sprungtor = NEW.sprungtor,

		spiolvl = NEW.spiolvl,
		computech = NEW.computech,
		waffentech = NEW.waffentech,
		schildtech = NEW.schildtech,
		rpz = NEW.rpz,
		energytech = NEW.energytech,
		hypertech = NEW.hypertech,
		vbt = NEW.vbt,
		impulse = NEW.impulse,
		hra = NEW.hra,
		lasertech = NEW.lasertech,
		iontech = NEW.iontech,
		plasmatech = NEW.plasmatech,
		forschungsnetz = NEW.forschungsnetz,
		expedition = NEW.expedition,
		gravi = NEW.gravi;

		END IF;
	  END";
		$this->run_mysql_query($query);
	}

	/**
	 * Checks if a URL is well formed or not.
	 * @param String $url
	 * @return boolean
	 */
	private static function is_valid_url($url) {
		// SCHEME
		$urlregex = "/";
		$urlregex .= "[^(https?)\:\/\/]?";

		// USER AND PASS (optional)
		$urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?";

		// HOSTNAME OR IP
		//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*"; // http://x = allowed (ex. http://localhost, http://routerlogin)
		$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // http://x.x = minimum
		//$urlregex .= "([a-z0-9+\$_-]+\.)*[a-z0-9+\$_-]{2,3}"; // http://x.xx(x) = minimum
		//use only one of the above

		// PORT (optional)
		//$urlregex .= "(\:[0-9]{2,5})?";
		// PATH (optional)
		//$urlregex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
		// GET Query (optional)
		//$urlregex .= "(\?[a-z+&\$_.-][a-z0-9;:@\/&%=+\$_.-]*)?";
		// ANCHOR (optional)
		//$urlregex .= "(#[a-z_.-][a-z0-9=&\.\+,+\$_.-]*)?\$";

		$urlregex .= "/i";
		// check
		if (preg_match($urlregex, $url)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * I collect information about the used MySQL version and PHP Version.
	 * I am using this information to know which versions I can skip.
	 * E.g. if only php5 would be used, I could use the php5 class system.
	 * If MySQL 5 or above would be used, I could use new MySQL 5 features.
	 */
	private function send_feedback() {
		$query = "SELECT VERSION() as version";
		$res = $this->run_mysql_query($query);
		if (!$res) return;
		$version = mysql_fetch_object($res);
		$version = $version->version;
		$fp = @fopen("http://www.galaxytool.eu/feedback.php?php_version=".substr(phpversion(),0,strpos(phpversion(), '-'))."&mysql_version=".substr($version,0,strpos($version, '-')),"r");
		@fclose($fp);
	}
}